<template>
  <div>
    <div class="form-box">
      <el-tabs v-model="activeName">
        <el-tab-pane label="用户登录" name="userLogin"></el-tab-pane>
        <el-tab-pane label="商家登录" name="restaurantLogin"></el-tab-pane>
      </el-tabs>
      <el-form label-width="100px" :model="loginForm" :rules="loginFormRules" ref="loginFormRef" status-icon>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data () {
    return {
      activeName: 'userLogin',
      loginForm: {
        username: '',
        password: ''
      },
      loginFormRules: {
        username: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 20,
            message: '请输入3~20个字符',
            triggers: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          },
          {
            min: 6,
            max: 20,
            message: '请输入6~20个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    resetForm () {
      this.$refs.loginFormRef.resetFields()
    },
    login () {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) {
          return false
        }
        const { data: res } = await this.$axios.post('/login/' + this.activeName, this.loginForm)
        if (res.status !== 2000) {
          return this.$message.error('登录失败')
        }
        this.$message.success('登录成功')
        window.sessionStorage.setItem('token', res.data.token)
        if (this.activeName === 'userLogin') {
          window.sessionStorage.setItem('clientId', '1')
          window.sessionStorage.setItem('uname', res.data.user.uname)
          window.sessionStorage.setItem('uid', res.data.user.uid)
          window.sessionStorage.setItem('phone', res.data.user.phone)
          window.sessionStorage.setItem('icon', res.data.user.icon)
          window.sessionStorage.setItem('exp', res.data.user.exp)
          window.sessionStorage.setItem('email', res.data.user.email)
          return this.$router.push('/home')
        }
        window.sessionStorage.setItem('clientId', '2')
        window.sessionStorage.setItem('rname', res.data.restaurant.rname)
        window.sessionStorage.setItem('address', res.data.restaurant.address)
        window.sessionStorage.setItem('cpicture', res.data.restaurant.cpicture)
        window.sessionStorage.setItem('detail', res.data.restaurant.detail)
        window.sessionStorage.setItem('email', res.data.restaurant.email)
        window.sessionStorage.setItem('icon', res.data.restaurant.icon)
        window.sessionStorage.setItem('kpicture', res.data.restaurant.kpicture)
        window.sessionStorage.setItem('phone', res.data.restaurant.phone)
        window.sessionStorage.setItem('rid', res.data.restaurant.rid)
        window.sessionStorage.setItem('rnickname', res.data.restaurant.rnickname)
        window.sessionStorage.setItem('variety', res.data.restaurant.variety)
        await this.$router.push('/merchant/home')
      })
    }
  }
}
</script>

<style scoped>
  .form-box {
    width: 500px;
    height: 500px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-250px, -250px);
  }
</style>
